package com.xxl.job.executor.service;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.kafka.KafkaProduceService;
import com.xxl.job.executor.model.CurrentInfoMjModel;

import com.xxl.job.executor.model.CurrentInfoMjModel;
import com.xxl.job.executor.model.SignalDefDhModel;
import com.xxl.job.executor.model.SignalMjDhModel;
import com.xxl.job.executor.repository.CurrentInfoRepository;
import com.xxl.job.executor.utils.ThreadLocalDateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

/**
 * @program: xxl-job
 * @description: 门禁发送redis
 * @author: XianJin.Lu
 * @create: 2021-09-08 11:03
 **/

@Service
public class MjService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DhService.class);
    @Autowired
    private SignalDefService signalDefService;
    @Autowired
    private CurrentInfoRepository currentInfoRepository;
    @Autowired
    private KafkaProduceService kafkaProduceService;

    @Autowired
    private BkJsonDataService bkJsonDataService;

    /**
     * @program: xxl-job
     * @description: 门禁发送redis
     * @author: XianJin.Lu
     * @create: 2021-09-08 11:03
     **/

    public void save(SignalMjDhModel automaticControlModel){

        //查询 redis中的  signalDefDhModel
        SignalMjDhModel signalMjDhModel=signalDefService.queryMjSignal(automaticControlModel.getGatherId(),
                automaticControlModel.getGatherResId(),automaticControlModel.getSignalType(),
                automaticControlModel.getPointAddr());


        if(signalMjDhModel==null){
            // LOGGER.error("门禁系统：未找到对应的信号配置");
            return;
        }
        String nDate="";
        nDate= ThreadLocalDateUtil.formatDate(new Date());

        String signalType  =signalMjDhModel.getSignalType();

        //新建实时存储的实体   其实是在信号表上多存了一个 value 状态值

        CurrentInfoMjModel currentInfoMjModel = new CurrentInfoMjModel();


        currentInfoMjModel.setValue(automaticControlModel.getSignalValue());
        currentInfoMjModel.setGatherResId(automaticControlModel.getGatherResId());
        currentInfoMjModel.setGatherId(automaticControlModel.getGatherId());

        currentInfoMjModel.setDevCode(signalMjDhModel.getDevCode());
        currentInfoMjModel.setSignalCode(signalMjDhModel.getCode());
        currentInfoMjModel.setSignalName(signalMjDhModel.getName());
        currentInfoMjModel.setSignalType(signalMjDhModel.getSignalType());
        currentInfoMjModel.setValunit("");
        currentInfoMjModel.setTime(nDate);
        currentInfoMjModel.setPointAddr(signalMjDhModel.getPointAddr());



        //根据实体去redis查询上次信号时间
        CurrentInfoMjModel currentInfoMjModel1=currentInfoRepository.queryMjCurrent(signalMjDhModel.getDevCode(),signalMjDhModel.getCode());

        if(currentInfoMjModel1==null){
            currentInfoMjModel.setLastTime(nDate);
        }else{
            currentInfoMjModel.setLastTime(currentInfoMjModel1.getTime());
        }
        currentInfoMjModel.setSendTime(ThreadLocalDateUtil.formatDate(new Date()));

        boolean flag=this.isSaveRedis(currentInfoMjModel,currentInfoMjModel1,signalType);
        if(flag) {
            //实时数据存入redis
            currentInfoRepository.save2(currentInfoMjModel);

            //发送kafka
            sends(currentInfoMjModel);

        }
    }


    /**
     * @program: xxl-job
     * @description: 发kafka
     * @author: XianJin.Lu
     * @create: 2021-09-08 11:03
     **/
    public void sends(CurrentInfoMjModel message) {

        try {
            this.kafkaProduceService.sendMjAlarmMessage(message);

        } catch (Exception e) {
            XxlJobLogger.log("发送告警信息失败:"+message.toString(), e);
        }
    }



    /**
     * @program: xxl-job
     * @description: 判断是否需要存redis
     * @author: XianJin.Lu
     * @create: 2021-09-08 11:03
     **/
    private boolean isSaveRedis(CurrentInfoMjModel automaticControlStatus,
                                CurrentInfoMjModel automaticControlStatus1, String signalType){
        int fa = 0;
        long iTimeDiffHour=0;
        String autoVal=automaticControlStatus.getValue();
        String oldautoVal=automaticControlStatus1.getValue();
        if(automaticControlStatus1 != null){
            if(!autoVal.equals(oldautoVal)){
                fa = 1;
            }
            iTimeDiffHour = (ThreadLocalDateUtil.parse(automaticControlStatus.getTime()).getTime() - ThreadLocalDateUtil.parse(automaticControlStatus1.getTime()).getTime()) / (1000 * 60* 20);
        }else{
            iTimeDiffHour = 2;
        }

        //fa>0  说明value值不同  状态改变了 要保存redis   或者超过一定事件刷新redis
        if ( fa > 0 || iTimeDiffHour > 1) {
            return true;
        }
        return false;
    }


}
